package cn.knet.excel.imp.knetexceltool.thread;

import cn.knet.excel.imp.knetexceltool.service.KnetBgdataCompanyBaseService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.LongAdder;

@Slf4j
public class KnetBgdataFilterThread implements Runnable {

    private LongAdder global_rowStarAdder = new LongAdder();
    private LongAdder global_queryCntAdder = new LongAdder();
    private LongAdder global_kwOkAdder = new LongAdder();
    private LongAdder global_kwFailAdder = new LongAdder();
    private LongAdder single_okAdder = new LongAdder();
    private LongAdder single_failAdder = new LongAdder();
    private KnetBgdataCompanyBaseService knetBgdataCompanyBaseService;
    private int pageSize;
    private int startNum;

    public KnetBgdataFilterThread(int startNum, LongAdder global_rowStarAdder, LongAdder global_queryCntAdder, LongAdder global_kwOkAdder, LongAdder global_kwFailAdder, KnetBgdataCompanyBaseService knetBgdataCompanyBaseService, int pageSize) {
        this.startNum = startNum;
        this.global_rowStarAdder = global_rowStarAdder;
        this.global_queryCntAdder = global_queryCntAdder;
        this.global_kwOkAdder = global_kwOkAdder;
        this.global_kwFailAdder = global_kwFailAdder;
        this.knetBgdataCompanyBaseService = knetBgdataCompanyBaseService;
        this.pageSize = pageSize;
    }

    @Override
    public void run() {
        long stime = System.currentTimeMillis();
        log.info("******** starting clean bgdatas ********");
        List<Map<String, String>> rstMap = new ArrayList<>();
        try {
            rstMap = knetBgdataCompanyBaseService.selectBgdataByEveryPage(startNum, pageSize);
            if (rstMap != null) {
                global_queryCntAdder.add(rstMap.size());
                knetBgdataCompanyBaseService.saveKeywordList(global_kwOkAdder, global_kwFailAdder, single_okAdder, single_failAdder, rstMap);
            } else {
                log.error("======分页查询全量企业数据，进行清洗操作时 返回结果列表为【空】");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("======分页查询全量企业数据，进行清洗操作时  查询异常操作异常！******");
        }
        long etime = System.currentTimeMillis();
        log.info(String.format("******** ending clean bgdatas ******** find_kwok_kwerr_size[%s_%s_%s] total_time[%s]秒", rstMap.size(), single_okAdder.sum(), single_failAdder.sum(), (etime - stime) / 1000d));
    }


}
